/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

class HiddenTemplatesActionGroup extends ActionGroup {
    private static final String[] HIDDEN_TEMPLATES = {
            "Component", "ComponentUnitTest",
            "Data", "DataUnitTest", "Factory", "FactoryUnitTest", "FactoryInterface"
    };
    private Logger logger = Logger.getInstance(getClass().getName());

    private ActionGroup existingGroup;

    public HiddenTemplatesActionGroup(ActionGroup newFromTemplateActionGroup) {
        this.existingGroup = newFromTemplateActionGroup;
    }

    public AnAction[] getChildren(AnActionEvent anActionEvent) {
        AnAction[] children = existingGroup.getChildren(anActionEvent);
        List ret = new ArrayList();
        for (int i = 0; i < children.length; i++) {
            AnAction child = children[i];
            Presentation presentation = child.getTemplatePresentation();
            String text = presentation.getText();
            logger.info("action text = " + text);
            boolean skip = false;
            if (text != null) {
                for (int j = 0; j < HIDDEN_TEMPLATES.length; j++) {
                    String hiddenTemplate = HIDDEN_TEMPLATES[j];
                    if (text.equals(hiddenTemplate)) {
                        skip = true;
                        break;
                    }
                }
            }
            if (!skip) {
                ret.add(child);
            }
        }
        return (AnAction[]) ret.toArray(new AnAction[0]);
    }

    public boolean displayTextInToolbar() {
        return existingGroup.displayTextInToolbar();
    }

    public void update(AnActionEvent anActionEvent) {
        existingGroup.update(anActionEvent);
    }

    protected void setShortcutSet(ShortcutSet shortcutSet) {
        try {
            Method method = existingGroup.getClass().getMethod(
                    "setShortcutSet", new Class[]{ShortcutSet.class});
            method.setAccessible(true);
            method.invoke(existingGroup, new Object[]{shortcutSet});
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void setDefaultIcon(boolean b) {
        existingGroup.setDefaultIcon(b);
    }

    public boolean isDefaultIcon() {
        return existingGroup.isDefaultIcon();
    }
}
